Overview of Timeouts in JavaScript

JavaScript provides for delayed execution. Using timeouts, you can schedule JavaScript tasks for future processing. For example, press the button below and wait for three seconds.

Timeouts are easy to set. Simply call the window.setTimeout() message with two arguments: a string containing JavaScript code and a millisecond delay. A three-second delay contains 3000 milliseconds.

window.setTimeout("PopIt('Timeout','Three seconds have passed')",3000)

The setTimeout call returns an id. Use this id to cancel the timeout with clearTimeout(). For example, press this "Press and Wait" button to create a three-second delay. Wait a few seconds and you will see that it works just like the example above. Now tap it again but hit the cancel button before three seconds have passed and stop the message from appearing. Wait a few seconds and you will see that nothing more happens. The timeout has been cancelled.

JSCTimeOut = window.setTimeout("PopIt('Timeout','Three seconds have passed')",1000)

and...

onClick="clearTimeout(JSCTimeOut)"

setTimeout(anExpression, milliSecs) Evaluate expressions after a delay, e.g., myID = setTimeout("myFunct()",5000) Used to create pending actions.
clearTimeout(anID) Clears a timeout set by setTimeout(), e.g., clearTimeout(myID). Used to cancel a pending action.
Copyright ©2000 by Charles River Media, All Rights Reserved